// import inView from 'in-view'
// import moduleLoading from '../components/common/loading/module-loading'
import utils from './utils'

export default function install(Vue) {
  // 富文本图片压缩
  Vue.directive('xhtml', (el, binding) => {
    const value = binding.value
    const ele = document.createElement('div')
    ele.innerHTML = value;
    [...ele.querySelectorAll('img')].forEach((img) => {
      if (/http|https/.test(img.src)) {
        img.src = utils.viewImage(img.src)
      }
    })
    el.innerHTML = ele.innerHTML
  })
  Vue.directive('highlight', (el, binding) => {
    const searchValue = binding.value.searchValue
    const oldValue = binding.value.oldValue
    const target = `<em>${searchValue}</em>`
    if (oldValue) {
      el.innerHTML = oldValue.replace(new RegExp(searchValue, 'gm'), target)
    } else {
      el.innerHTML = binding.value.oldValue
    }
  })
  Vue.directive('numfocus', {
    // 指令的定义
    inserted: function(el) {
      el.addEventListener('input', function() {
        // 进行验证
        checkedfun(el)
      })
      function checkedfun(el) {
        try {
          console.log(el)
          let indexValue = el.value
          let reg = new RegExp(/^(0|[1-9]\d)/)
          if (!reg.test(indexValue)) {
            el.value = indexValue.replace(/[^\d]/g, '')
            el.dispatchEvent(new Event('input')) // 调用input事件使vue v-model绑定更新
            return
          } else if (indexValue.length > 0 && indexValue.indexOf('0') === 0) {
            el.value = indexValue.replace(/\b(0+)/gi, '')
            el.dispatchEvent(new Event('input'))// 调用input事件使vue v-model绑定更新
            return
          } else {
            return
          }
        } catch (error) {
          return
        }
      }
    }
  })
}
